crash bug fixes, mod is playable at this point

Missing some features from the pre-rewrite version such as playing
battle music, using bow/arrows, limiting number of combatants in
battle...
This commit is contained in:
Stephen Seo 2018-09-12 14:43:59 +09:00
parent 3d6354e808
commit 5999808859
13 changed files with 191 additions and 62 deletions

View file

@ -5,12 +5,13 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.Combatant;
import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleDecision;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
@ -125,7 +126,10 @@ public class BattleGui extends GuiScreen
public void turnEnd()
{
TurnBasedMinecraftMod.currentBattle.setState(Battle.State.DECISION);
if(TurnBasedMinecraftMod.currentBattle != null)
{
TurnBasedMinecraftMod.currentBattle.setState(Battle.State.DECISION);
}
timeRemaining.set((int)TurnBasedMinecraftMod.BattleDecisionTime.getSeconds());
setState(MenuState.MAIN_MENU);
}
@ -148,36 +152,36 @@ public class BattleGui extends GuiScreen
{
case MAIN_MENU:
info = "What will you do?";
buttonList.add(new GuiButton(ButtonAction.ATTACK.getValue(), width*2/7 - 40, height - 120, 80, 20, "Attack"));
buttonList.add(new GuiButton(ButtonAction.DEFEND.getValue(), width*3/7 - 40, height - 120, 80, 20, "Defend"));
buttonList.add(new GuiButton(ButtonAction.ITEM.getValue(), width*4/7 - 40, height - 120, 80, 20, "Item"));
buttonList.add(new GuiButton(ButtonAction.FLEE.getValue(), width*5/7 - 40, height - 120, 80, 20, "Flee"));
buttonList.add(new GuiButton(ButtonAction.ATTACK.getValue(), width*2/7 - 30, height - 120, 60, 20, "Attack"));
buttonList.add(new GuiButton(ButtonAction.DEFEND.getValue(), width*3/7 - 30, height - 120, 60, 20, "Defend"));
buttonList.add(new GuiButton(ButtonAction.ITEM.getValue(), width*4/7 - 30, height - 120, 60, 20, "Item"));
buttonList.add(new GuiButton(ButtonAction.FLEE.getValue(), width*5/7 - 30, height - 120, 60, 20, "Flee"));
break;
case ATTACK_TARGET:
info = "Who will you attack?";
int y = 50;
int y = 30;
for(Map.Entry<Integer, Combatant> e : TurnBasedMinecraftMod.currentBattle.getSideAEntrySet())
{
if(e.getValue().entity != null)
{
buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width/4 - 100, y, e.getValue().entity.getName(), e.getKey()));
buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width/4 - 60, y, 120, 20, e.getValue().entity.getName(), e.getKey()));
}
else
{
buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width/4 - 100, y, "Unknown", e.getKey()));
buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width/4 - 60, y, 120, 20, "Unknown", e.getKey()));
}
y += 20;
}
y = 50;
y = 30;
for(Map.Entry<Integer, Combatant> e : TurnBasedMinecraftMod.currentBattle.getSideBEntrySet())
{
if(e.getValue().entity != null)
{
buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width*3/4 - 100, y, e.getValue().entity.getName(), e.getKey()));
buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width*3/4 - 60, y, 120, 20, e.getValue().entity.getName(), e.getKey()));
}
else
{
buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width*3/4 - 100, y, "Unknown", e.getKey()));
buttonList.add(new EntitySelectionButton(ButtonAction.ATTACK_TARGET.getValue(), width*3/4 - 60, y, 120, 20, "Unknown", e.getKey()));
}
y += 20;
}
@ -214,6 +218,11 @@ public class BattleGui extends GuiScreen
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
if(TurnBasedMinecraftMod.currentBattle == null)
{
drawHoveringText("Waiting...", width / 2 - 50, height / 2);
return;
}
if(TurnBasedMinecraftMod.currentBattle.getState() == Battle.State.DECISION && timeRemaining.get() > 0)
{
long nextInstant = System.nanoTime();
@ -230,7 +239,25 @@ public class BattleGui extends GuiScreen
super.drawScreen(mouseX, mouseY, partialTicks);
drawHoveringText(info, width / 2 - 50, height - 50);
String timeRemainingString = "Time remaining: ";
int timeRemainingInt = timeRemaining.get();
if(timeRemainingInt > 8)
{
timeRemainingString += "\u00A7a";
}
else if(timeRemainingInt > 4)
{
timeRemainingString += "\u00A7e";
}
else
{
timeRemainingString += "\u00A7c";
}
timeRemainingString += Integer.toString(timeRemainingInt);
int stringWidth = Minecraft.getMinecraft().fontRenderer.getStringWidth(timeRemainingString);
Minecraft.getMinecraft().fontRenderer.drawString(timeRemainingString, width/2 - stringWidth/2, 5, 0xFFFFFFFF);
stringWidth = Minecraft.getMinecraft().fontRenderer.getStringWidth(info);
Minecraft.getMinecraft().fontRenderer.drawString(info, width/2 - stringWidth/2, 20, 0xFFFFFFFF);
}
@Override
@ -276,6 +303,10 @@ public class BattleGui extends GuiScreen
if(button instanceof ItemSelectionButton)
{
PacketHandler.INSTANCE.sendToServer(new PacketBattleDecision(TurnBasedMinecraftMod.currentBattle.getId(), Battle.Decision.SWITCH_ITEM, ((ItemSelectionButton)button).itemStackID));
if(((ItemSelectionButton)button).itemStackID >= 0 && ((ItemSelectionButton)button).itemStackID < 9)
{
Minecraft.getMinecraft().player.inventory.currentItem = ((ItemSelectionButton)button).itemStackID;
}
setState(MenuState.WAITING);
}
else

View file

@ -0,0 +1,35 @@
package com.seodisparate.TurnBasedMinecraft.common;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class AttackEventHandler
{
@SubscribeEvent
public void entityAttacked(LivingAttackEvent event)
{
if(event.getEntity().world.isRemote)
{
TurnBasedMinecraftMod.logger.debug("isRemote");
return;
}
else if(TurnBasedMinecraftMod.battleManager == null)
{
TurnBasedMinecraftMod.battleManager = new BattleManager(TurnBasedMinecraftMod.logger);
}
if(!(event.getSource().getTrueSource() == null || event.getSource().getTrueSource().equals(TurnBasedMinecraftMod.attackingEntity)) && TurnBasedMinecraftMod.battleManager.checkAttack(event))
{
TurnBasedMinecraftMod.logger.debug("Canceled LivingAttackEvent between " + TurnBasedMinecraftMod.attackingEntity + " and " + event.getEntity());
event.setCanceled(true);
}
else
{
TurnBasedMinecraftMod.logger.debug("Did not cancel attack");
}
if(TurnBasedMinecraftMod.attackingDamage < (int) event.getAmount())
{
TurnBasedMinecraftMod.attackingDamage = (int) event.getAmount();
}
}
}

View file

@ -11,11 +11,11 @@ import java.util.Queue;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
@ -588,7 +588,14 @@ public class Battle
{
// attack
// TODO damage via bow and arrow
// have player look at attack target
final Entity nextEntity = next.entity;
final Entity targetEntity = target.entity;
Minecraft.getMinecraft().addScheduledTask(() -> {
((EntityPlayerMP)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, Utility.yawDirection(nextEntity.posX, nextEntity.posZ, targetEntity.posX, targetEntity.posZ), Utility.pitchDirection(nextEntity.posX, nextEntity.posY, nextEntity.posZ, targetEntity.posX, targetEntity.posY, targetEntity.posZ));
});
TurnBasedMinecraftMod.attackingEntity = next.entity;
TurnBasedMinecraftMod.attackingDamage = 0;
((EntityPlayer)next.entity).attackTargetEntityWithCurrentItem(target.entity);
TurnBasedMinecraftMod.attackingEntity = null;
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, next.entity.getEntityId(), target.entity.getEntityId(), TurnBasedMinecraftMod.attackingDamage);
@ -844,6 +851,7 @@ public class Battle
c.decision = Decision.UNDECIDED;
}
state = State.DECISION;
undecidedCount.set(players.size());
healthCheck();
sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0);
break;

View file

@ -5,7 +5,7 @@ import java.util.Collection;
import java.util.Hashtable;
import java.util.Map;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import org.apache.logging.log4j.Logger;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
@ -16,9 +16,11 @@ public class BattleManager
private int IDCounter = 0;
protected Map<Integer, Battle> battleMap;
private Thread updaterThread;
private Logger logger;
public BattleManager()
public BattleManager(Logger logger)
{
this.logger = logger;
battleMap = new Hashtable<Integer, Battle>();
updaterThread = new Thread(new BattleUpdater(this));
updaterThread.start();
@ -40,6 +42,39 @@ public class BattleManager
{
return false;
}
// check if ignore battle in config
EntityInfo entityInfo = TurnBasedMinecraftMod.config.getMatchingEntityInfo(event.getEntity());
if(entityInfo != null && (TurnBasedMinecraftMod.config.isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle))
{
// attacked entity ignores battle
for(Battle b : battleMap.values())
{
if(b.hasCombatant(event.getSource().getTrueSource().getEntityId()))
{
logger.debug("Attack Canceled: attacked ignores battle but attacker in battle");
return true;
}
}
logger.debug("Attack Not Canceled: attacked ignores battle");
return false;
}
entityInfo = TurnBasedMinecraftMod.config.getMatchingEntityInfo(event.getSource().getTrueSource());
if(entityInfo != null && (TurnBasedMinecraftMod.config.isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle))
{
// attacker entity ignores battle
for(Battle b : battleMap.values())
{
if(b.hasCombatant(event.getEntity().getEntityId()))
{
logger.debug("Attack Canceled: attacker ignores battle but attacked in battle");
return true;
}
}
logger.debug("Attack Not Canceled: attacker ignores battle");
return false;
}
// check if one is in battle
Entity inBattle = null;
Entity notInBattle = null;
@ -52,6 +87,7 @@ public class BattleManager
if(inBattle != null)
{
// both combatants are in battle
logger.debug("Attack Canceled: both are in battle");
return true;
}
else
@ -66,6 +102,7 @@ public class BattleManager
if(inBattle != null)
{
// both combatants are in battle
logger.debug("Attack Canceled: both are in battle");
return true;
}
else
@ -88,6 +125,11 @@ public class BattleManager
sideA.add(event.getEntity());
sideB.add(event.getSource().getTrueSource());
createBattle(sideA, sideB);
logger.debug("Attack Not Canceled: new battle created");
}
else
{
logger.debug("Attack Not Canceled: neither are in battle or players");
}
return false;
}
@ -102,6 +144,7 @@ public class BattleManager
battle.addCombatantToSideA(notInBattle);
}
logger.debug("Attack Canceled: one is in battle");
return true;
}

View file

@ -2,8 +2,6 @@ package com.seodisparate.TurnBasedMinecraft.common;
import java.util.Comparator;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;

View file

@ -20,7 +20,6 @@ import javax.xml.stream.XMLStreamReader;
import org.apache.logging.log4j.Logger;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.EntityInfo.Category;
public class Config
@ -404,7 +403,7 @@ public class Config
return null;
}
EntityInfo matching = entityInfoMap.get(entity.getClass().getName());
if(matching.classType.isInstance(entity))
if(matching != null && matching.classType.isInstance(entity))
{
for(Class c : matching.conflictingTypes)
{
@ -453,4 +452,9 @@ public class Config
return configVersion;
}
public boolean isIgnoreBattleType(Category type)
{
return ignoreBattleTypes.contains(type);
}
}

View file

@ -1,13 +1,10 @@
package com.seodisparate.TurnBasedMinecraft;
package com.seodisparate.TurnBasedMinecraft.common;
import java.time.Duration;
import org.apache.logging.log4j.Logger;
import com.seodisparate.TurnBasedMinecraft.client.BattleGui;
import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.BattleManager;
import com.seodisparate.TurnBasedMinecraft.common.Config;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleDecision;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
@ -15,6 +12,7 @@ import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleRequest
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler;
import net.minecraft.entity.Entity;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
@ -38,12 +36,12 @@ public class TurnBasedMinecraftMod
private static int CONFIG_FILE_VERSION = 0;
private static Logger logger;
private static BattleManager battleManager;
protected static Logger logger;
protected static BattleManager battleManager;
private static int packetHandlerID = 0;
public static Entity attackingEntity;
public static int attackingDamage = 0;
public static Config config;
protected static Entity attackingEntity;
protected static int attackingDamage = 0;
protected static Config config;
public static Battle currentBattle = null;
public static BattleGui currentBattleGui = null;
@ -59,14 +57,8 @@ public class TurnBasedMinecraftMod
public void init(FMLInitializationEvent event)
{
currentBattle = null;
if(event.getSide() == Side.SERVER)
{
battleManager = new BattleManager();
}
else
{
battleManager = null;
}
battleManager = null;
config = new Config(logger);
// register packets
PacketHandler.INSTANCE.registerMessage(
@ -90,32 +82,16 @@ public class TurnBasedMinecraftMod
packetHandlerID++,
Side.CLIENT);
logger.debug("INIT");
// register event handler(s)
MinecraftForge.EVENT_BUS.register(new AttackEventHandler());
}
@EventHandler
public void postInit(FMLPostInitializationEvent event)
{
if(battleManager != null)
{
config = new Config(logger);
}
logger.debug("POSTINIT");
}
@SubscribeEvent
public void entityAttacked(LivingAttackEvent event)
{
if(battleManager == null || event.getEntity().world.isRemote)
{
return;
}
if(!event.getSource().getTrueSource().equals(attackingEntity) && battleManager.checkAttack(event))
{
logger.debug("Canceled LivingAttackEvent between " + attackingEntity + " and " + event.getEntity());
event.setCanceled(true);
}
attackingDamage = (int) event.getAmount();
}
public static BattleManager getBattleManager()
{

View file

@ -0,0 +1,31 @@
package com.seodisparate.TurnBasedMinecraft.common;
public class Utility
{
public static float yawDirection(double posX, double posZ, double targetX, double targetZ)
{
double radians = Math.atan2(targetZ - posZ, targetX - posX);
radians = (radians - Math.PI / 2.0);
if(radians < 0.0)
{
radians += Math.PI * 2.0;
}
return (float)(radians * 180.0 / Math.PI);
}
public static float pitchDirection(double posX, double posY, double posZ, double targetX, double targetY, double targetZ)
{
double diffX = targetX - posX;
double diffY = targetY - posY;
double diffZ = targetZ - posZ;
double distance = Math.sqrt(diffX * diffX + diffZ * diffZ);
if(Math.abs(diffY) < 0.1)
{
return 0;
}
else
{
return (float)(-Math.atan(diffY / distance) * 180.0 / Math.PI);
}
}
}

View file

@ -1,7 +1,7 @@
package com.seodisparate.TurnBasedMinecraft.common.networking;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.Battle.Decision;
import io.netty.buffer.ByteBuf;

View file

@ -3,7 +3,7 @@ package com.seodisparate.TurnBasedMinecraft.common.networking;
import java.util.ArrayList;
import java.util.Collection;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;

View file

@ -3,9 +3,9 @@ package com.seodisparate.TurnBasedMinecraft.common.networking;
import java.util.HashMap;
import java.util.Map;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.client.BattleGui;
import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
@ -327,8 +327,11 @@ public class PacketBattleMessage implements IMessage
}
break;
case TURN_END:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(
if(TurnBasedMinecraftMod.currentBattle != null)
{
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(
"The turn ended!"));
}
if(TurnBasedMinecraftMod.currentBattleGui != null)
{
TurnBasedMinecraftMod.currentBattleGui.turnEnd();

View file

@ -1,7 +1,7 @@
package com.seodisparate.TurnBasedMinecraft.common.networking;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

View file

@ -1,6 +1,6 @@
package com.seodisparate.TurnBasedMinecraft.common.networking;
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;