Skip to content

Commit

Permalink
regain backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
AndKram committed May 13, 2019
1 parent 3cd2b88 commit a20590d
Show file tree
Hide file tree
Showing 10 changed files with 602 additions and 277 deletions.
10 changes: 8 additions & 2 deletions Malmo/samples/Python_examples/inventory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def getContainerXML():
while world_state.is_mission_running:
world_state = agent_host.getWorldState()
if world_state.number_of_rewards_since_last_state > 0:
total_reward += world_state.rewards[-1].getValue()
reward = world_state.rewards[-1].getValue()
if reward != 0:
print("Got reward of " + str(reward))
total_reward += reward
if world_state.number_of_observations_since_last_state > 0:
obs = json.loads(world_state.observations[-1].text)

Expand Down Expand Up @@ -306,7 +309,10 @@ def getContainerXML():
# Mission has ended.
# Get final reward:
if world_state.number_of_rewards_since_last_state > 0:
total_reward += world_state.rewards[-1].getValue()
reward = world_state.rewards[-1].getValue()
if reward != 0:
print("Got final reward of " + str(reward))
total_reward += reward

test_passed = True
if False in list(completed_boxes.values()):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
// --------------------------------------------------------------------------------------------------
// // Copyright (c) 2016 Microsoft Corporation
// //
// // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// // associated documentation files (the "Software"), to deal in the Software without restriction,
// // including without limitation the rights to use, copy, modify, merge, publish, distribute,
// // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// // furnished to do so, subject to the following conditions:
// //
// // The above copyright notice and this permission notice shall be included in all copies or
// // substantial portions of the Software.
// //
// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// // --------------------------------------------------------------------------------------------------

package com.microsoft.Malmo.MissionHandlers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
Expand All @@ -34,151 +13,95 @@
import com.microsoft.Malmo.Schemas.AgentQuitFromCollectingItem;
import com.microsoft.Malmo.Schemas.BlockOrItemSpecWithDescription;
import com.microsoft.Malmo.Schemas.MissionInit;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;

/**
* Quits the mission when the agent has collected the right amount of items. The count on the item collection is absolute.
*/
public class AgentQuitFromCollectingItemImplementation extends HandlerBase implements IWantToQuit {
private AgentQuitFromCollectingItem params;
private HashMap<String, Integer> collectedItems;
private List<ItemQuitMatcher> matchers;
private String quitCode = "";
private boolean wantToQuit = false;
public class AgentQuitFromCollectingItemImplementation extends HandlerBase implements IWantToQuit
{
AgentQuitFromCollectingItem params;
List<ItemQuitMatcher> matchers;
String quitCode = "";
boolean wantToQuit = false;

public static class ItemQuitMatcher extends RewardForItemBase.ItemMatcher {
public static class ItemQuitMatcher extends RewardForItemBase.ItemMatcher
{
String description;

ItemQuitMatcher(BlockOrItemSpecWithDescription spec) {
ItemQuitMatcher(BlockOrItemSpecWithDescription spec)
{
super(spec);
this.description = spec.getDescription();
}

String description() {
String description()
{
return this.description;
}
}

@Override
public boolean parseParameters(Object params) {
if (!(params instanceof AgentQuitFromCollectingItem))
public boolean parseParameters(Object params)
{
if (params == null || !(params instanceof AgentQuitFromCollectingItem))
return false;

this.params = (AgentQuitFromCollectingItem) params;
this.params = (AgentQuitFromCollectingItem)params;
this.matchers = new ArrayList<ItemQuitMatcher>();
for (BlockOrItemSpecWithDescription bs : this.params.getItem())
this.matchers.add(new ItemQuitMatcher(bs));
return true;
}

@Override
public boolean doIWantToQuit(MissionInit missionInit) {
public boolean doIWantToQuit(MissionInit missionInit)
{
return this.wantToQuit;
}

@Override
public String getOutcome() {
public String getOutcome()
{
return this.quitCode;
}

@Override
public void prepare(MissionInit missionInit) {
public void prepare(MissionInit missionInit)
{
MinecraftForge.EVENT_BUS.register(this);
collectedItems = new HashMap<String, Integer>();
}

@Override
public void cleanup() {
public void cleanup()
{
MinecraftForge.EVENT_BUS.unregister(this);
}

@SubscribeEvent
public void onGainItem(GainItemEvent event) {
if (event.stack != null && event.cause == 0)
checkForMatch(event.stack);
}

@SubscribeEvent
public void onPickupItem(EntityItemPickupEvent event) {
if (event.getItem() != null && event.getEntityPlayer() instanceof EntityPlayerMP)
checkForMatch(event.getItem().getEntityItem());
public void onGainItem(GainItemEvent event)
{
checkForMatch(event.stack);
}

@SubscribeEvent
public void onItemCraft(PlayerEvent.ItemCraftedEvent event) {
if (event.player instanceof EntityPlayerMP && !event.crafting.isEmpty())
checkForMatch(event.crafting);
}

@SubscribeEvent
public void onItemSmelt(PlayerEvent.ItemSmeltedEvent event) {
if (event.player instanceof EntityPlayerMP && !event.smelting.isEmpty())
checkForMatch(event.smelting);
}

/**
* Checks whether the ItemStack matches a variant stored in the item list. If
* so, returns true, else returns false.
*
* @param is The item stack
* @return If the stack is allowed in the item matchers and has color or
* variants enabled, returns true, else false.
*/
private boolean getVariant(ItemStack is) {
for (ItemQuitMatcher matcher : matchers) {
if (matcher.allowedItemTypes.contains(is.getItem().getUnlocalizedName())) {
if (matcher.matchSpec.getColour() != null && matcher.matchSpec.getColour().size() > 0)
return true;
if (matcher.matchSpec.getVariant() != null && matcher.matchSpec.getVariant().size() > 0)
return true;
}
}

return false;
}

private void addCollectedItemCount(ItemStack is) {
boolean variant = getVariant(is);

if (variant) {
int prev = (collectedItems.get(is.getUnlocalizedName()) == null ? 0
: collectedItems.get(is.getUnlocalizedName()));
collectedItems.put(is.getUnlocalizedName(), prev + is.getCount());
} else {
int prev = (collectedItems.get(is.getItem().getUnlocalizedName()) == null ? 0
: collectedItems.get(is.getItem().getUnlocalizedName()));
collectedItems.put(is.getItem().getUnlocalizedName(), prev + is.getCount());
public void onPickupItem(EntityItemPickupEvent event)
{
if (event.getItem() != null && event.getItem().getEntityItem() != null)
{
ItemStack stack = event.getItem().getEntityItem();
checkForMatch(stack);
}
}

private int getCollectedItemCount(ItemStack is) {
boolean variant = getVariant(is);

if (variant)
return (collectedItems.get(is.getUnlocalizedName()) == null) ? 0 : collectedItems.get(is.getUnlocalizedName());
else
return (collectedItems.get(is.getItem().getUnlocalizedName()) == null) ? 0
: collectedItems.get(is.getItem().getUnlocalizedName());
}

private void checkForMatch(ItemStack is) {
int savedCollected = getCollectedItemCount(is);
if (is != null) {
for (ItemQuitMatcher matcher : this.matchers) {
if (matcher.matches(is)) {
if (savedCollected != 0) {
if (is.getCount() + savedCollected >= matcher.matchSpec.getAmount()) {
this.quitCode = matcher.description();
this.wantToQuit = true;
}
} else if (is.getCount() >= matcher.matchSpec.getAmount()) {
this.quitCode = matcher.description();
this.wantToQuit = true;
}
private void checkForMatch(ItemStack is)
{
if (is != null)
{
for (ItemQuitMatcher matcher : this.matchers)
{
if (matcher.matches(is))
{
this.quitCode = matcher.description();
this.wantToQuit = true;
}
}

addCollectedItemCount(is);
}
}
}
Loading

0 comments on commit a20590d

Please sign in to comment.